import java.util.PriorityQueue;

class Solution {
    public int lastStoneWeight(int[] stones) {
        // 创建大根堆
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
        for (int x : stones) {
            heap.offer(x);
        }
        while (heap.size() > 1) {
            int a = heap.poll();
            int b = heap.poll();
            if (a > b) heap.offer(a - b);
        }
        return heap.isEmpty() ? 0 : heap.poll();
    }
}